package com.lucastex.grails.fileuploader

class DownloadController {
	
	def messageSource
	
    def index = { 
	
		UFile ufile = UFile.get(params.id)
		if (!ufile) {
			def msg = messageSource.getMessage("fileupload.download.nofile", [params.id] as Object[], request.locale)
			log.debug msg
			flash.message = msg
			redirect controller: params.errorController, action: params.errorAction
			return
		}
		
		def file = new File(ufile.path)
		if (file.exists()) {
			log.debug "Serving file id=[${ufile.id}] for the ${ufile.downloads} to ${request.remoteAddr}"
			ufile.downloads++
			ufile.save()
            params.contentDisposition = params.contentDisposition? params.contentDisposition: "attachment"
			response.setContentType("application/octet-stream;charset=UTF-8")
            response.setHeader("Pragma", "No-cache"); 
            response.setHeader("Cache-Control", "No-cache"); 
            response.setDateHeader("Expires", 0);
			response.setHeader("Content-disposition", "${params.contentDisposition}; filename=${new String(file.name.getBytes('gbk'),'iso8859-1')}") 
            response.setHeader("Content-Length", "${file.size()}");  

            BufferedInputStream bufferedInput = file.newInputStream() 
            BufferedOutputStream output = new BufferedOutputStream(response.outputStream);
            
            byte[] buffer = new byte[2048];
            int bytesRead = 0;
            while (-1 != (bytesRead = bufferedInput.read(buffer))) {
                output << buffer
            }
            response.flushBuffer()

            bufferedInput.close()
            output.close()
			return
		} else {
			def msg = messageSource.getMessage("fileupload.download.filenotfound", [ufile.name] as Object[], request.locale)
			log.error msg
			flash.message = msg
			redirect controller: params.errorController, action: params.errorAction
			return
		}
	}
}
